package com.itrip.itripauth.util;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: itrip-parent
 * @description: 分页
 * @author: joker
 * @create: 2020-03-13 12:15
 **/
public class PageUtil<T> {
    private List<T> rows;    //实体类列表
    private boolean first;     //是否首
    private boolean last;      //是否尾页
    private Integer pageSize;   //总记录数
    private Integer pageCount;    //总页数
    private Integer total;
    private Integer size;  //每页记录数
    private Integer curPage;    //当前页
    private Integer beginPos;       //起始位置

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public boolean isFirst() {
        return first;
    }

    public void setFirst(boolean first) {
        this.first = first;
    }

    public boolean isLast() {
        return last;
    }

    public void setLast(boolean last) {
        this.last = last;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageCount() {
        return pageCount;
    }

    public void setPageCount(Integer pageCount) {
        this.pageCount = pageCount;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Integer getCurPage() {
        return curPage;
    }

    public void setCurPage(Integer curPage) {
        this.curPage = curPage;
    }


    @Override
    public String toString() {
        return "PageUtil{" +
                "rows=" + rows +
                ", first=" + first +
                ", last=" + last +
                ", pageSize=" + pageSize +
                ", pageCount=" + pageCount +
                ", total=" + total +
                ", size=" + size +
                ", curPage=" + curPage +
                ", beginPos=" + beginPos +
                '}';
    }


    public PageUtil(Integer page, Integer size, List<T> list) {
        List<T> list1 = list.stream().skip((page - 1) * size).limit(size).collect(Collectors.toList());

        int length = list.size();
        this.first = (page == 0);//是否第一页
        this.beginPos = page;
        this.last = (page == (length - 1) / size);//是否最后一页
        this.pageCount = ((length - 1) / size + 1);//总页数
        this.total = (length);//总elements
        this.size = (size);//每页多少elements
        this.rows = (list1);//内容
        this.pageSize = (list1.size());//当前页elements
        this.curPage = (page);//当前页数，第一页是0
    }

}
